﻿using Sunny.UI;
using System.Diagnostics;
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using CoreShop.Model;
using Newtonsoft.Json;
using CoreShop.Library;
using static E3.CSharpZipLib.Zip.ExtendedUnixData;
using System.Threading;
using System.Xml;

namespace CoreShop.App
{
    public partial class frmMainPage : UIHeaderAsideMainFooterFrame
    {
        private bool loginFlag = false;        

        public frmMainPage()
        {
            InitializeComponent();

            try
            {
                //设置关联
                Aside.TabControl = MainTabControl;

                //SysAutoUpdate sysUpdate = new SysAutoUpdate();
                if (true)
                {
                    //增加页面到Main
                    AddPage(new fpgDataMonitor(), (int)GlobalEnumVars.MainPageIndex.Page1001);                  

                    if (AppConstVars.sysStore.channel > 0)
                    {
                        //AddPage(new fpgSignalMonitor(), (int)GlobalEnumVars.MainPageIndex.Page1004);
                    }
                    AddPage(new fpgPivotMonitor(), (int)GlobalEnumVars.MainPageIndex.Page1005);
                    AddPage(new fpgOpraMonitor(), (int)GlobalEnumVars.MainPageIndex.Page1006);

                    ////设置Header节点索引
                    Aside.CreateNode("数据控制台", (int)GlobalEnumVars.MainPageIndex.Page1001);
                    if (AppConstVars.systemsetup.isKitchen)
                    {
                        //Aside.CreateNode("打印机监控", (int)GlobalEnumVars.MainPageIndex.Page1002);
                        //Aside.CreateNode("厨房打印监控", (int)GlobalEnumVars.MainPageIndex.Page1003);
                    }
                    if (AppConstVars.sysUser.channel > 0)
                    {
                        Aside.CreateNode("线上餐厅监控", (int)GlobalEnumVars.MainPageIndex.Page1004);
                    }
                    Aside.CreateNode("前台系统监控", (int)GlobalEnumVars.MainPageIndex.Page1005);
                    Aside.CreateNode("其它功能", (int)GlobalEnumVars.MainPageIndex.Page1006);

                    ////显示默认界面
                    Aside.SelectFirst();

                    loginFlag = true;

                    string updateListXml = Application.StartupPath + "\\UpdateListEx.xml";
                    XmlFiles serverXmlFiles = new XmlFiles(updateListXml);
                    XmlNodeList newNodeList = serverXmlFiles.GetNodeList("AutoUpdater/Files");//5+1+a+s+p+x
                    var exChangeVersion = newNodeList.Item(2).Attributes["Ver"].Value.Trim(); ;

                    this.Text += string.Concat("  V", exChangeVersion);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Initialize出错：" + ex.Message);
            }           
        }
              
        public void SelectPage()
        {
            Aside.SelectPage((int)GlobalEnumVars.MainPageIndex.Page1002);
        }

        private void Aside_MenuItemClick(System.Windows.Forms.TreeNode node, NavMenuItem item, int pageIndex)
        {
            Footer.Text = "PageIndex: " + pageIndex;
        }

        private void frmMainPage_Load(object sender, System.EventArgs e)
        {
            try
            {
                if (loginFlag)
                {
                    var aa = AppConstVars.SynDataFlag;
                    if (AppConstVars.sysUser.channel > 0)
                    {
                        //SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1004, "传值给页面");
                    }
                    if (AppConstVars.systemsetup.isKitchen)
                    {
                        //SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1002, "传值给页面");
                        //SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1003, "传值给页面");
                    }
                    //var pivot = new SignalPivotModel() { code = (int)GlobalEnumVars.SignalPivotCode.Code100 };
                    //SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1005, pivot);

                    //SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1006, "传值给页面");
                    //Thread.Sleep(3000);
                    SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1001, AppConstVars.SynDataFlag);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Load出错：" + ex.Message);
            }
        }

        public void SendParamToPrint()
        {
            var pivot = new PosPivotModel() { code = (int)GlobalEnumVars.SignalPivotCode.Code101 };
            SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1003, pivot);//转到方法：fpgPrintMonitor_ReceiveParams;
        }

        public void SendPivotMessage(PosPivotModel entry)
        {
            //var value = JsonConvert.SerializeObject(entry.data);
            this.Invoke(new Action(() =>
            {
                switch (entry.code)
                {
                    case (int)GlobalEnumVars.SignalPivotCode.Code101:
                        SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1003, entry);//转到方法：fpgPrintMonitor_ReceiveParams
                        break;
                }

                //SendParamToPage((int)GlobalEnumVars.MainPageIndex.Page1005, entry);//转到方法：fpgPivotMonitor_ReceiveParams
            }));
        }

        private void uiMarkLabel1_Click(object sender, System.EventArgs e)
        {

        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void frmMainPage_FormClosing(object sender, FormClosingEventArgs e)
        {
 
            if (AppConstVars.ExitFlag == false)
            {
                e.Cancel = true;
                this.TopMost = false;
                this.Hide();
            }
        }

        private void frmMainPage_Shown(object sender, EventArgs e)
        {
            
        }
    }
}
